#include <stdio.h>
#include "mywindows.h"
#include "disp.h"

//背景颜色
#define MY_GAME_BACK_STYLE 0x30
//边框颜色
#define MY_FRAME_STYLE 0x80
//游戏中心背景色
#define MY_GAME_CENTER_STYLE 0xee

// 分数中心区域背景色
#define MY_SCORE_CENTER_STYLE 0x70
// 分数字体颜色
#define MY_SCORE_FONT_STYLE 0x8e
// 分数值颜色
#define MY_SCORE_VALUE_STYLE 0x7e
// 100分 对应的块
#define MY_100_SCORE_STYLE 0xce
// 10分 对应的块
#define MY_10_SCORE_STYLE 0xae
// 1分 对应的块
#define MY_1_SCORE_STYLE 0xde

// 等级中心区域
#define MY_GRADE_CENTER_STYLE 0x70
// 等级字体颜色
#define MY_GRADE_FONT_STYLE 0x8e
// 等级值颜色
#define MY_GRADE_VALUE_STYLE 0x66

// 信息中心区域颜色 白底黑字
#define MY_INFO_CENTER_STYLE 0xf0
// "说明" 两个字 颜色
#define MY_INFO_FONT_STYLE 0x8e
// 游戏结束 字体颜色 黄底红字
#define MY_GAME_FONT_STYLE 0xed

//蛇的颜色
#define SNAKE_STYLE 0x95
//食物的颜色
#define FOOD_STYLE 0xc9

//绘制背景
void dispDrawGameBackground()
{
    setColor(MY_GAME_BACK_STYLE);
    for (int i=0;i<30;i++)
    {
        setPosition(0,i);
        printf("%100s","");
    }
}

//绘制矩形
void drawRect(int x,int y,int width,int height,int frameStyle,int centerStyle)
{
    setColor(frameStyle);
    setPos(x,y);
    for(int i=0;i<width;i++)
    {
        printf("%2s","");
    }
    setPos(x,y+height-1);
    for(int i=0;i<width;i++)
    {
        printf("%2s","");
    }
    for (int i=0;i<height;i++)
    {
        setPos(x,y+i);
        printf("%2s","");
    }
    for (int i=0;i<height;i++)
    {
        setPos(x+width-1,y+i);
        printf("%2s","");
    }
    setColor(centerStyle);
    for (int i=x+1;i<(x+width)-1;i++)
        for (int j=y+1;j<(y+height)-1;j++)
        {
            setPosition(i,j);
            printf("%s","  ");
        }
}

//绘制中心区域
void dispDrawGameCenter()
{
    drawRect(12,2,26,26,MY_FRAME_STYLE,MY_GAME_CENTER_STYLE);
}

//绘制分数区域
void dispDrawScore(int score)
{
    drawRect(1,2,10,14,MY_FRAME_STYLE,MY_SCORE_CENTER_STYLE);
    setText(MY_SCORE_FONT_STYLE);
    setPos(5,2);
    printf("分数");
    setText(MY_SCORE_VALUE_STYLE);
    setPos(5,4);
    printf("%d",score);
    int i=2,j=6;
    while(score>=100)
    {
        setText(MY_100_SCORE_STYLE);
        setPos(i,j);
        printf("%2s","");
        i+=2;
        if(i>9)
            {
                if (j>=7)
                    i=2;
                else
                    i=3;
                j+=1;
            }
        score-=100;
    }
    i=2;j+=1;
    while(score>=10)
    {
        setText(MY_10_SCORE_STYLE);
        setPos(i,j);
        printf("%2s","");
        i+=2;
        if(i>9)
            {
                if (j>=10)
                    i=2;
                else
                    i=3;
                j+=1;
            }
        score-=10;
    }
    i=2;j+=1;
    while(score>=1)
    {
        setText(MY_1_SCORE_STYLE);
        setPos(i,j);
        printf("%2s","");
        i+=2;
        if(i>9)
            {
                if (j>=13)
                    i=2;
                else
                    i=3;
                j+=1;
            }
        score-=1;
    }
}

//绘制数字 drawNum(4, 19, degree,MY_DEGREE_STYLE);
void drawNum(int x, int y, int number, int numStyle)
{
    if(number < 0 || number > 9)
        return;
    if(numStyle < 0 || numStyle > 0xFF)
        return;
    setText(numStyle);
    switch(number)
    {
    case 0:
        setPos(x, y);
        printf("%8s","");
        setPos(x, y+6);
        printf("%8s","");
        setPos(x, y+3);
        printf("%2s","");
        setPos(x+3, y+3);
        printf("%2s","");
        //左
        setPos(x, y+1);
        printf("%2s","");
        setPos(x, y+2);
        printf("%2s","");
        setPos(x, y+4);
        printf("%2s","");
        setPos(x, y+5);
        printf("%2s","");
        //右
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        break;
    case 1:
        //右
        setPos(x+3, y);
        printf("%2s","");
        setPos(x+3, y+3);
        printf("%2s","");
        setPos(x+3, y+6);
        printf("%2s","");
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        break;
    case 2:
        setPos(x, y);
        printf("%8s","");
        setPos(x, y+3);
        printf("%8s","");
        setPos(x, y+6);
        printf("%8s","");
        //左
        setPos(x, y+4);
        printf("%2s","");
        setPos(x, y+5);
        printf("%2s","");
        //右
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        break;
    case 3:
        setPos(x, y);
        printf("%8s","");
        setPos(x, y+3);
        printf("%8s","");
        setPos(x, y+6);
        printf("%8s","");
        //右
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        //
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        break;
    case 4:
        setPos(x, y);
        printf("%2s","");
        setPos(x+3, y);
        printf("%2s","");
        setPos(x, y+3);
        printf("%8s","");
        setPos(x+3, y+6);
        printf("%2s","");
        //右
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        //
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        //左
        setPos(x, y+1);
        printf("%2s","");
        setPos(x, y+2);
        printf("%2s","");
        break;
    case 5:
        setPos(x, y);
        printf("%8s","");
        setPos(x, y+3);
        printf("%8s","");
        setPos(x, y+6);
        printf("%8s","");
        //左
        setPos(x, y+1);
        printf("%2s","");
        setPos(x, y+2);
        printf("%2s","");
        //右
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        break;
    case 6:
        setPos(x, y);
        printf("%8s","");
        setPos(x, y+3);
        printf("%8s","");
        setPos(x, y+6);
        printf("%8s","");
        //左
        setPos(x, y+1);
        printf("%2s","");
        setPos(x, y+2);
        printf("%2s","");
        setPos(x, y+4);
        printf("%2s","");
        setPos(x, y+5);
        printf("%2s","");
        //右
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        break;
    case 7:
        setPos(x, y);
        printf("%8s","");
        setPos(x+3, y+3);
        printf("%2s","");
        setPos(x+3, y+6);
        printf("%2s","");
        //右
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        //
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        break;
    case 8:
        setPos(x, y);
        printf("%8s","");
        setPos(x, y+3);
        printf("%8s","");
        setPos(x, y+6);
        printf("%8s","");
        //左
        setPos(x, y+1);
        printf("%2s","");
        setPos(x, y+2);
        printf("%2s","");
        setPos(x, y+4);
        printf("%2s","");
        setPos(x, y+5);
        printf("%2s","");
        //右
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        break;
case 9:
        setPos(x, y);
        printf("%8s","");
        setPos(x, y+3);
        printf("%8s","");
        setPos(x, y+6);
        printf("%8s","");
        //左
        setPos(x, y+1);
        printf("%2s","");
        setPos(x, y+2);
        printf("%2s","");
        //右
        setPos(x+3, y+1);
        printf("%2s","");
        setPos(x+3, y+2);
        printf("%2s","");
        setPos(x+3, y+4);
        printf("%2s","");
        setPos(x+3, y+5);
        printf("%2s","");
        break;
    }
}
//绘制等级区域
void dispDrawGrade(int grade)
{
    drawRect(1,17,10,11,MY_FRAME_STYLE,MY_GRADE_CENTER_STYLE);
    setText(MY_GRADE_FONT_STYLE);
    setPos(5, 17);
    printf("等级");
    setText(MY_GAME_BACK_STYLE);
    drawNum(4, 19, grade, MY_GRADE_VALUE_STYLE);
}

//绘制提示信息1-游戏初始提示
void dispDrawInitInfo()
{
    drawRect(39,2,10,26,MY_FRAME_STYLE,MY_INFO_CENTER_STYLE);
    setText(MY_INFO_FONT_STYLE);
    setPos(43, 2);
    printf("说明");
    setText(MY_INFO_CENTER_STYLE);
    setPos(42, 4);
    printf("游戏说明");
    setPos(42,6);
    printf("[游戏]");
    setPos(41, 7);
    printf("W w:向上移动");
    setPos(41, 8);
    printf("S s:向下移动");
    setPos(41, 9);
    printf("A a:向左移动");
    setPos(41, 10);
    printf("D d:向右移动");
    setPos(41, 11);
    printf("Q q:退出游戏");
    setPos(42,13);
    printf("[控制]");
    setPos(41, 14);
    printf("P p:暂停游戏");
    setPos(41,15);
    printf("W w:增加难度");
    setPos(41,16);
    printf("S s:降低难度");
    setPos(41, 20);
    printf("作者: 石子怡");
    setPos(41, 21);
    printf("版本: V1.0");
    setPos(41, 22);
    printf("日期: 2021.12");

}

//绘制提示信息2-游戏运行提示
void dispDrawRunInfo()
{
    drawRect(39,2,10,26,MY_FRAME_STYLE,MY_INFO_CENTER_STYLE);
    setText(MY_INFO_FONT_STYLE);
    setPos(43, 2);
    printf("说明");
    setText(MY_INFO_CENTER_STYLE);
    setPos(43, 4);
    printf("开始");
    setPos(41, 7);
    printf("W w:向上移动");
    setPos(41, 8);
    printf("S s:向下移动");
    setPos(41, 9);
    printf("A a:向左移动");
    setPos(41, 10);
    printf("D d:向右移动");
    setPos(41, 11);
    printf("Q q:退出游戏");
    setPos(41, 12);
    printf("P p:暂停游戏");
    setPos(41, 20);
    printf("作者: 石子怡");
    setPos(41, 21);
    printf("版本: V1.0");
    setPos(41, 22);
    printf("日期: 2021.12");
}

//绘制提示信息3-游戏暂停提示
void dispDrawPauseInfo()
{
    drawRect(39,2,10,26,MY_FRAME_STYLE,MY_INFO_CENTER_STYLE);
    setText(MY_INFO_FONT_STYLE);
    setPos(43, 2);
    printf("说明");
    setText(MY_INFO_CENTER_STYLE);
    setPos(43, 4);
    printf("暂停");
    setPos(41, 7);
    printf("P p:开始游戏");
    setPos(41, 8);
    printf("Q q:退出游戏");
    setPos(41, 20);
    printf("作者: 石子怡");
    setPos(41, 21);
    printf("版本: V1.0");
    setPos(41, 22);
    printf("日期: 2021.12");
}

//绘制提示信息4-游戏结束提示
void dispDrawOverInfo(int score)
{
    drawRect(39,2,10,26,MY_FRAME_STYLE,MY_INFO_CENTER_STYLE);
    setText(MY_INFO_FONT_STYLE);
    setPos(43, 2);
    printf("说明");
    setText(MY_GAME_BACK_STYLE);
    setText(MY_INFO_CENTER_STYLE);
    setPos(43, 4);
    printf("停止");
    setPos(41, 7);
    printf("W w:增加难度");
    setPos(41, 8);
    printf("S s:降低难度");
    setPos(41, 11);
    printf("Q q:退出游戏");
    setPos(41, 12);
    printf("P p:开始游戏");
    setPos(41, 20);
    printf("作者: 石子怡");
    setPos(41, 21);
    printf("版本: V1.0");
    setPos(41, 22);
    printf("日期: 2021.12");
    setText(MY_GAME_BACK_STYLE);
    setText(MY_GAME_FONT_STYLE);
    setPos(19,14);
    printf("游戏结束，当前得分：%d", score);
}
//绘制蛇
void dispDrawSnake(Snake *s)
{
    //擦除原先蛇尾坐标
    if(preX!=-1 && preY!=-1)
    {
        setColor(MY_GAME_CENTER_STYLE);
        setPosition(preX+13,preY+3);
        printf("%2s","");
    }
    setColor(SNAKE_STYLE);
    //获取第一个结点
    SnakeNode *node = s->head;
    if(preX!=-1 && preY!=-1)
    {
        setPos(node->x+13,node->y+3);
        printf("%2s","");
        return;
    }
    //绘制蛇
    while(node != NULL)
    {
    //获取x y坐标
    //调整光标到合适位置
    setPosition(node->x+13,node->y+3);
    //输出空白字符
    printf("%2s","");
    //调整node指向下一个结点
    node=node->next;
    }

}

//绘制食物
void dispDrawFood(Food *f)
{
    setColor(FOOD_STYLE);
    setPosition(f->x+13,f->y+3);
    printf("%2s","");
}
    //显示模块初始化
void dispInitGame()
{
    //绘制窗体背景色
    dispDrawGameBackground();
    //绘制游戏中心区域
    dispDrawGameCenter();
    //绘制分数区域
    dispDrawScore(score);
    //绘制等级区域
    dispDrawGrade(grade);
    //绘制信息提示
    dispDrawInitInfo();
}
